//
//  LYVideoInstance.m
//  LYLiveApp
//
//  Created by QD on 2018/3/1.
//  Copyright © 2018年 览益信息科技. All rights reserved.
//

#import "LYVideoInstance.h"
#import "OpenCONSTS.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>

@interface LYVideoInstance ()<VHallMoviePlayerDelegate>

@end

@implementation LYVideoInstance

+ (LYVideoInstance *)sharedInstance
{
    static LYVideoInstance *sharedInstance = nil;
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

- (id)init {
    
    self = [super init];
    
    if ( self ) {
    }
    return self;
}

- (VHallMoviePlayer *)moviePlayer {
    
    if (nil == _moviePlayer) {
        _moviePlayer =  [[VHallMoviePlayer alloc] initWithDelegate:self];
    }
    return _moviePlayer;
}


#pragma mark ----- VHMoviePlayerDelegate
/**
 *  播放连接成功
 */

- (void)connectSucceed:(VHMoviePlayer*)moviePlayer info:(NSDictionary*)info {
    LYLog(@"LYVideoInstance ========播放连接成功");
  
    [LYVideoInstance sharedInstance].isPlaySuccess = YES;
    [[NSNotificationCenter defaultCenter] postNotificationName:kLYVideoInstancePlaySuccess object:nil];
}

/**
 *  缓冲开始回调
 */
- (void)bufferStart:(VHMoviePlayer*)moviePlayer info:(NSDictionary*)info {
    LYLog(@"LYVideoInstance ========缓冲开始回调");
    [[NSNotificationCenter defaultCenter] postNotificationName:kLYVideoInstanceBeginLoading object:nil];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

/**
 *  缓冲结束回调
 */
-(void)bufferStop:(VHMoviePlayer*)moviePlayer info:(NSDictionary*)info {
    LYLog(@"LYVideoInstance ========缓冲结束回调");
    
    [[NSNotificationCenter defaultCenter] postNotificationName:kLYVideoInstanceFinishLoading object:nil];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    
}
/**
 *  下载速率的回调
 *
 *  @param moviePlayer
 *  @param info        下载速率信息 单位kbps
 */
- (void)downloadSpeed:(VHMoviePlayer*)moviePlayer info:(NSDictionary*)info {
    //    LYLog(@"下载速率的回调");
    
    [LYUtil hideHUDTo:self.moviePlayer.moviePlayerView];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    
}

/**
 *  网络状态的回调
 *
 *  @param moviePlayer
 *  @param info        网络状态信息  content的值越大表示网络越好
 */
- (void)netWorkStatus:(VHMoviePlayer*)moviePlayer info:(NSDictionary*)info {
    LYLog(@"LYVideoInstance ============网络状态的回调");
    
    LYLog(@"netWorkStatus:%f",[info[@"content"]floatValue]);
}
/**
 *  cdn 发生切换时的回调
 *
 *  @param moviePlayer
 *  @param info
 */
- (void)cdnSwitch:(VHMoviePlayer*)moviePlayer info:(NSDictionary*)info {
    LYLog(@"LYVideoInstance ==============发生切换时的回调");
    
    [LYVideoInstance sharedInstance].isPlaySuccess = NO;

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [[NSNotificationCenter defaultCenter] postNotificationName:kLYVideoInstanceBeginLoading object:nil];
    
}

/**
 *  播放时错误的回调
 *
 *  @param livePlayErrorType 解盘错误类型
 */
- (void)playError:(LivePlayErrorType)livePlayErrorType info:(NSDictionary*)info {
    NSString * msg = @"";
    void (^resetStartPlay)(NSString * msg) = ^(NSString * msg){
        LYLog(@"LYVideoInstance  msg -----  %@",msg);
    };
    
    switch (livePlayErrorType) {
        case kLivePlayParamError:
        {
            msg = @"参数错误";
            resetStartPlay(msg);
            [self _handleErrorAction];
        }
            break;
        case kLivePlayRecvError:
        {
            msg = @"对方已经停止解盘";
            resetStartPlay(msg);
            [self _handleErrorAction];
        }
            break;
        case kLivePlayCDNConnectError:
        {
            msg = @"服务器任性...连接失败";
            resetStartPlay(msg);
            [self _handleErrorAction];
        }
            break;
        case kLivePlayGetUrlError:
        {
            msg = @"获取服务器地址报错";
            resetStartPlay(msg);
            LYLog(@"error  ------%@",info[@"content"]);
            [self _handleErrorAction];
        }
            break;
        default:
            break;
    }
}

/**
 *  code       含义
 *  10030    身份验证出错
 *  10402    当前活动ID错误
 *  10404    KEY值验证出错
 *  10046    当前活动已结束
 *  10047    您已被踢出，请联系活动组织者
 *  10048    活动现场太火爆，已超过人数上限
 */

#pragma mark -----VHallMoviePlayerDelegate
/**
 *  包含文档 获取翻页图片路径
 *
 *  @param changeImage  图片更新
 */
- (void)PPTScrollNextPagechangeImagePath:(NSString*)changeImagePath {
    
}
/**
 *  获取视频播放模式
 *
 *  @param playMode  视频播放模式
 */
- (void)VideoPlayMode:(VHallMovieVideoPlayMode)playMode {
    LYLog(@"model %ld",(long)playMode);
    
}
/**
 *  获取视频活动状态
 *
 *  @param playMode  视频活动状态
 */
- (void)ActiveState : (VHallMovieActiveState)activeState {
    switch (activeState) {
        case VHallMovieActiveStateLive:
            LYLog(@"解盘");
            break;
        case VHallMovieActiveStateReservation:
            LYLog(@"预约");
            break;
        case VHallMovieActiveStateEnd:
            LYLog(@"结束");
            break;
        case VHallMovieActiveStateReplay:
            LYLog(@"回放");
            break;
        default:
            break;
    }
    
}

/**
 *  该解盘支持的清晰度列表
 *
 *  @param definitionList  支持的清晰度列表
 */
- (void)VideoDefinitionList: (NSArray*)definitionList {
    LYLog(@"LYVideoInstance =========definitionList %@",definitionList);
    self.definitionArray = [NSArray arrayWithArray:definitionList];
}

/**
 *  解盘结束消息 如果视频正在播放 等下一次loading 解盘即结束
 *
 *  解盘结束消息
 */
- (void)LiveStoped {
    LYLog(@"LYVideoInstance ============解盘结束");
    [self.moviePlayer stopPlay];
    [LYLocalData sharedInstance].roomInfo.living = @(0);
    [[NSNotificationCenter defaultCenter] postNotificationName:kVedioLiveControllerRemoveSmallVideoScreen object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:kLYVideoInstanceFinishPlaying object:nil];

}

- (void)_handleErrorAction {
    [self.moviePlayer stopPlay];
    [LYVideoInstance sharedInstance].isPlaySuccess = NO;
    [[NSNotificationCenter defaultCenter] postNotificationName:kLYVideoInstanceHandelError object:nil];
}


@end
